/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.policy;

import java.util.List;
import java.util.Random;

import net.javlov.Action;
import net.javlov.ContinuousAction;
import net.javlov.Policy;
import net.javlov.State;

/**
 * A policy that doesn't pay attention to the state but selects a random action on every
 * invocation of getAction(State), according to the supplied random number generator.
 * 
 * @author Matthijs Snel
 *
 */
public abstract class RandomPolicy implements Policy {
	
	protected Random rng;
	
	protected RandomPolicy() {}
	
	public static class Continuous extends RandomPolicy {

		private int dim;
		private double minRange, maxRange;
		private ContinuousAction template;
		
		public Continuous(ContinuousAction action, Random rng) {
			template = action;
			minRange = action.getMinRange();
			maxRange = action.getMaxRange();
			dim = template.getDimensionality();
			this.rng = rng;
		}
		
		@Override
		public Action getAction(State s) {
			return generateRandomAction();
		}

		@Override
		public Action getAction(double[] qvalues) {
			return generateRandomAction();
		}
		
		protected Action generateRandomAction() {
			double vals[] = new double[dim];
			for ( int i = 0; i < dim; i++ )
				vals[i] = minRange + rng.nextDouble()*(maxRange - minRange);
			template.setValues(vals);
			return template;
		}
	}
	
	
	public static class Discrete extends RandomPolicy {
		private List<? extends Action> actionPool;
		private int size;
		
		public Discrete(List<? extends Action> actionPool, Random rng) {
			this.actionPool = actionPool;
			size = actionPool.size();
			this.rng = rng;
		}
		
		@Override
		public Action getAction(State s) {
			return actionPool.get(rng.nextInt(size));
		}

		@Override
		public Action getAction(double[] qvalues) {
			return actionPool.get(rng.nextInt(size));
		}
	}
}
